package com.itlzh.mall.user.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itlzh.mall.common.dto.UserReceiveAddressDTO;
import com.itlzh.mall.common.entity.UserReceiveAddress;
import com.itlzh.mall.user.mapper.IUserReceiveAddressMapper;
import com.itlzh.mall.user.service.IUserReceiveAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserReceiveAddressServiceImpl extends ServiceImpl<IUserReceiveAddressMapper, UserReceiveAddress> implements IUserReceiveAddressService {
    @Autowired
    private IUserReceiveAddressMapper  baseMapper;

    @Override
    public List<UserReceiveAddressDTO> listByUserId(Long userId) {
        List<UserReceiveAddress> list = new LambdaQueryChainWrapper<>(baseMapper).eq(UserReceiveAddress::getUserId, userId).list();
        for (UserReceiveAddress address : list) {
            address.setPhoneNumber(address.getEncryptPhoneNumber());
        }
        List<UserReceiveAddressDTO> listVO=new ArrayList<>();
        for (UserReceiveAddress userReceiveAddress : list) {
            UserReceiveAddressDTO UVO=new UserReceiveAddressDTO();
            BeanUtils.copyProperties(userReceiveAddress,UVO);
            listVO.add(UVO);
        }
        return listVO;


    }

    @Override
    public boolean save(UserReceiveAddressDTO userReceiveAddressDTO) {
        UserReceiveAddress userReceiveAddress = new UserReceiveAddress();
        BeanUtils.copyProperties(userReceiveAddressDTO, userReceiveAddress);
        int insert = baseMapper.insert(userReceiveAddress);
        return insert > 0;
    }
}
